<?php
include_once('config/config.php');
$auth = check_auth();
include_once('./config/logout.php');
// 判断是否有post请求
if ($_POST) {
	// 接收post函数
	$depname = $_POST['depname'] ?? '';
	// 判断输入框是否为空
	if (empty($depname)) {
		showMsg('部门名必填');
	}
	// 通过sql语句查询部门名是否存在
	$depSql = "SELECT * FROM 'pre_dep' WHERE 'name' = '$depname'";
	$depData = find($depSql);
	if ($depData) {
		showMsg('部门名已存在');
	}
	$data = [
		'name' => $depname
	];
	$result = add('dep', $data);
	if ($result['code'] === 1) {
		showMsg('添加部门成功', 'depList.php');
	} else {
		// 输出错误信息
		echo $result['data'];
		exit;
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<style>
	/* .row-fluid [class*="span"]{
    margin-left: 0;
  } */
</style>

<head>
	<?php include_once('common/meta.php'); ?>
</head>
<link rel="stylesheet" href="./assets/lib/city-picker/city-picker.css">

<body>
	<?php include_once('common/header.php'); ?>
	<?php include_once('common/menu.php'); ?>
	<div class="content">
		<div class="header">
			<h1 class="page-title">添加部门</h1>
		</div>
		<ul class="breadcrumb">
			<li><a href="index.html">Home</a> <span class="divider">/</span></li>
			<li class="active">Index</li>
		</ul>

		<div class="container-fluid">
			<div class="row-fluid">

				<div class="btn-toolbar">
					<button class="btn btn-primary" onClick="location='list.html'"><i class="icon-list"></i> 部门列表</button>
					<div class="btn-group">
					</div>
				</div>

				<div class="well">
					<div id="myTabContent" class="tab-content">
						<div class="tab-pane active in" id="home">
							<form method="post">

								<label>部门名称</label>
								<input type="text" name="depname" class="input-xxlarge">

								<label></label>
								<input class="btn btn-primary" type="submit" value="提交" />
							</form>
						</div>
					</div>
				</div>

				<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
						<h3 id="myModalLabel">Delete Confirmation</h3>
					</div>
					<div class="modal-body">

						<p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
					</div>
					<div class="modal-footer">
						<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
						<button class="btn btn-danger" data-dismiss="modal">Delete</button>
					</div>
				</div>

				<?php include_once('common/footer.php') ?>

			</div>
		</div>
	</div>
</body>

</html>
<?php include_once('common/script.php') ?>
<script src="./assets/lib/city-picker/city-picker.data.min.js"></script>
<script src="./assets/lib/city-picker/city-picker.min.js"></script>
<script>
	// 模拟点击上传框
	$('#upload').click(function() {
		$('#avatar').click();
	});

	// 监听上传框的值是否有变化
	$('#avatar').change(function() {
		let file = $(this)[0].files[0];

		if (file) {
			let Reader = new FileReader();

			Reader.readAsDataURL(file);

			Reader.onload = function() {
				$('#upload').attr('src', Reader.result);
			}
		}
	});
</script>